{% macro link(entity) -%}
	{% if entity is mapping and ('title' in entity or 'albumtitle' in entity) %}
		{% set name = entity.title or entity.albumtitle %}
	{% else %}
		{% set name = entity %}
	{% endif %}

	<a href="{{ url(entity) }}">{{ name | e }}</a>
{%- endmacro %}

{% macro links(entities, restrict_amount=False) -%}
	{% if entities is none or entities == [] %}
		{{ settings["DEFAULT_ALBUM_ARTIST"] }}
	{% elif entities.__len__() > 3 and restrict_amount %}
	    {{ link(entities[0]) }} et al.
	{% else %}
		{% for entity in entities -%}
			{{ link(entity) }}{{ ", " if not loop.last }}
		{%- endfor %}
	{% endif %}
{%- endmacro %}



{% macro url(entity) %}
  {% if entity is mapping and 'albumtitle' in entity -%}
	  {{ mlj_uri.create_uri("/album",{'album':entity}) }}
	{% elif entity is mapping and 'artists' in entity -%}
		{{ mlj_uri.create_uri("/track",{'track':entity}) }}
	{%- else -%}
		{{ mlj_uri.create_uri("/artist",{'artist':entity}) }}
	{%- endif %}
{% endmacro %}



{% macro link_scrobbles(keydicts,amount=None,percent=None) -%}

	<a href='{{ mlj_uri.create_uri("/scrobbles",*keydicts) }}'>
		{%- if percent is not none -%}
			<div style='width:{{ percent }}%;'></div>
		{%- elif amount is not none -%}
			{{ amount }}
		{%- endif -%}

	</a>
{%- endmacro %}


{% macro link_rank(filterkeys,specialkeys,timerange,rank=None,percent=None) %}

	{% if 'track' in filterkeys %}
		{% set url = mlj_uri.create_uri("/charts_tracks",{'timerange':timerange}) %}
	{% elif 'album' in filterkeys %}
		{% set url = mlj_uri.create_uri("/charts_albums",{'timerange':timerange}) %}
	{% elif 'artist' in filterkeys %}
		{% set url = mlj_uri.create_uri("/charts_artists",{'timerange':timerange},specialkeys) %}
	{% endif %}

	{% set rankclass = {1:'gold',2:'silver',3:'bronze'}[rank] or "" %}

	<a href='{{ url }}'>
		{% if percent is not none %}
			<div class=" {{ rankclass }}" style='width: {{ percent }}%;'></div>
		{% else %}
			{% if rank is not none %}#{{ rank }}{% else %}n/a{% endif %}
		{% endif %}
	</a>
{%- endmacro %}


{% macro link_search(entity) -%}
	{% set searchstr = (entity.artists + [entity.title]) | join(" ") | urlencode %}

	{% set searchprovider = settings['TRACK_SEARCH_PROVIDER'] | lower | replace(' ','') %}
	{% set searchproviders =
		{
			"youtube":'https://www.youtube.com/results?search_query=',
			"youtubemusic":'https://music.youtube.com/search?q=',
			"spotify":'https://open.spotify.com/search/',
			"tidal":'https://listen.tidal.com/search/tracks?q=',
			"soundcloud":'https://soundcloud.com/search?q=',
			"amazonmusic":'https://music.amazon.com/search/',
			"deezer":'https://www.deezer.com/search/',
			"apple":'https://music.apple.com/us/search/song?term=',
			"beatport":'https://beatport.com/search?q=',
			"bandcamp":'https://bandcamp.com/search?q=',
			"qobuz":'https://www.qobuz.com/us-en/search?q='
		}
	%}

	{% set link = searchproviders.get(searchprovider,'https://duckduckgo.com/?q=') + searchstr %}

	<a href="{{ link }}">
		<svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" class="w-6 h-6" viewBox="0 0 20 20"><path d="M18 3a1 1 0 00-1.196-.98l-10 2A1 1 0 006 5v9.114A4.369 4.369 0 005 14c-1.657 0-3 .895-3 2s1.343 2 3 2 3-.895 3-2V7.82l8-1.6v5.894A4.37 4.37 0 0015 12c-1.657 0-3 .895-3 2s1.343 2 3 2 3-.895 3-2V3z"/></svg>
	</a>
{%- endmacro %}
